package com.ruoyi.common.utils;

import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.Units;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
public class QrCodeImageWriteHandler implements CellWriteHandler {

    private final Map<String, byte[]> imageMap;
    private final int imageWidth;
    private final int pixelConversionFactor;

    public QrCodeImageWriteHandler(Map<String, byte[]> imageMap) {
        this(imageMap, 60, 32);
    }

    public QrCodeImageWriteHandler(Map<String, byte[]> imageMap, int imageWidth, int pixelConversionFactor) {
        this.imageMap = imageMap;
        this.imageWidth = imageWidth;
        this.pixelConversionFactor = pixelConversionFactor;
    }

    @Override
    public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                       WriteCellData<?> cellData, Cell cell, Head head,
                                       Integer relativeRowIndex, Boolean isHead) {
        if (!isHead && cellData.getType() == CellDataTypeEnum.STRING && imageMap.containsKey(cellData.getStringValue())) {
            cellData.setType(CellDataTypeEnum.EMPTY); // 清空文本，防止显示{}
        }
    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 List<WriteCellData<?>> cellDataList, Cell cell, Head head,
                                 Integer relativeRowIndex, Boolean isHead) {
        if (isHead || CollectionUtils.isEmpty(cellDataList)) {
            return;
        }

        WriteCellData<?> cellData = cellDataList.get(0);
        if (cellData.getType() != CellDataTypeEnum.EMPTY) {
            return;
        }

        String placeholder = cellData.getStringValue();
        if (!imageMap.containsKey(placeholder)) {
            return;
        }

        byte[] imageBytes = imageMap.get(placeholder);
        insertImage(cell.getSheet(), cell, imageBytes);
    }

    private void insertImage(Sheet sheet, Cell cell, byte[] imageBytes) {
        Drawing<?> drawing = sheet.getDrawingPatriarch();
        if (drawing == null) {
            drawing = sheet.createDrawingPatriarch();
        }

        int pictureIndex = sheet.getWorkbook().addPicture(imageBytes, Workbook.PICTURE_TYPE_PNG);
        CreationHelper helper = sheet.getWorkbook().getCreationHelper();
        ClientAnchor anchor = helper.createClientAnchor();

        int columnIndex = cell.getColumnIndex();
        int rowIndex = cell.getRowIndex();
        anchor.setCol1(columnIndex);
        anchor.setCol2(columnIndex);
        anchor.setRow1(rowIndex);
        anchor.setRow2(rowIndex);
        anchor.setDx1(Units.pixelToEMU(1));
        anchor.setDx2(Units.pixelToEMU(imageWidth));
        anchor.setDy1(Units.pixelToEMU(1));
        anchor.setDy2(Units.pixelToEMU(imageWidth));
        anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);

        drawing.createPicture(anchor, pictureIndex);
        //sheet.setColumnWidth(columnIndex, imageWidth * pixelConversionFactor);
    }
}
